package com.gouda.backend.controller.rbac;

import com.themis.dto.BackendSystemUserDto;
import com.themis.dto.FrontFunctionDto;
import com.themis.dto.FunctionTreeDto;
import com.themis.service.SystemFunctionService;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.gouda.backend.controller.AbstractController;
import com.gouda.backend.exception.ErrorCode;
import com.gouda.core.service.systemManage.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by KGL on 2015/8/14.
 */
@Controller
@RequestMapping("/systemFunction")
public class BackendSystemFunctionController extends AbstractController {
    @Autowired
    private SystemFunctionService functionService;
    @Autowired
    private SystemUserService userService;

    @RequestMapping("/getSystemFunctions")
    public @ResponseBody
    Map<String, Object> getSystemFunctions(HttpServletRequest request){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        List<FunctionTreeDto> functionTree = functionService.initFunctions();
        resultMap.put("code", operateResult.getCode());
        resultMap.put("data", functionTree);
        return resultMap;
    }

    @RequestMapping("/getChildren")
    public @ResponseBody
    Map<String, Object> getChildren(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String parentId = request.getParameter("parentId");

        String loginUserId = super.getAdminUserId(request);
        BackendSystemUserDto loginUser = userService.getUserById(loginUserId);
        List<FrontFunctionDto> children = functionService.loadChildrenByParentId(parentId, loginUser);
        resultMap.put("code", operateResult.getCode());
        resultMap.put("data", children);
        return resultMap;
    }

    @RequestMapping("/modifyFunction")
    public @ResponseBody
    Map<String, Object> modifyUser(@RequestBody FrontFunctionDto functionDto){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final Long functionId = functionDto.getFunctionId();
        final String functionName = functionDto.getFunctionName();
        final String functionUrl = functionDto.getFunctionUrl();
        if (null == functionId) {
            resultMap.put("code", ErrorCode.EC_400004.getCode());
            resultMap.put("msg", ErrorCode.EC_400004.getMessage());
            return resultMap;
        }
        functionService.modifyFunctionInfo(functionId, functionName, functionUrl);

        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

    @RequestMapping("/addFunction")
    public @ResponseBody
    Map<String, Object> addFunction(@RequestBody FrontFunctionDto functionDto, HttpServletRequest request){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String loginUserId = this.getAdminUserId(request);
        final Long parentId = functionDto.getParentId();
        final String functionCode = functionDto.getAddFunctionCode();
        final String functionName = functionDto.getAddFunctionName();
        final String functionUrl = functionDto.getAddFunctionUrl();
        final int type = functionDto.getType();

        if (Strings.isNullOrEmpty(loginUserId) || Strings.isNullOrEmpty(functionCode) || Strings.isNullOrEmpty(functionName)) {
            resultMap.put("code", ErrorCode.EC_400004.getCode());
            resultMap.put("msg", ErrorCode.EC_400004.getMessage());
            return resultMap;
        }
        if(functionService.checkExist(functionCode)){
            resultMap.put("code", ErrorCode.EC_400005.getCode());
            resultMap.put("msg", ErrorCode.EC_400005.getMessage());
            return resultMap;
        }
        functionService.addFunction(loginUserId, parentId, functionCode, functionName, functionUrl, type);

        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

}
